from django.http import JsonResponse, HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.views import View

from carapp.cartmanager import SessionCartManager
from userapp.models import UserInfo, Area, Address
from utils.code import *
from django.core.serializers import serialize


# Create your views here.
class Registerview(View):
    def get(self,request):
        return render(request,'register.html')
    def post(self,request):
        #获取请求参数
        uname = request.POST.get('uname','')
        pwd = request.POST.get('pwd','')


        #插入数据库
        user = UserInfo.objects.create(uname=uname,pwd=pwd)

        #判断是否注册成功
        if user:
            #将用户信息存放到session对象中
            request.session['user'] = user

            return HttpResponseRedirect('/user/center')

        return HttpResponseRedirect('user/register/')

class CheckUnameview(View):
    def get(self,request):
        #获取请求参数
        uname = request.GET.get('uname','')

        #根据用户名去数据库中查询
        userList = UserInfo.objects.filter(uname=uname)

        flag = False

        #判断是否存在
        if userList:
            flag = True

        return JsonResponse({'flag':flag})


class Centerview(View):
    def get(self,request):
        return render(request,'center.html')


class Logoutview(View):
    def post(self,request):
        #删除session中所有的数据/登录用户信息
        if 'user' in request.session:
            del request.session['user']

        return JsonResponse({'delflag':True})


class Loginview(View):
    def get(self,request):

        #获取请求参数
        red = request.GET.get('redirct','')


        return render(request,'login.html',{'redirect':red})


    def post(self,request):
        #获取请求参数
        uname = request.POST.get('uname','')
        pwd = request.POST.get('pwd','')

        #查询数据库中是否存在
        userList = UserInfo.objects.filter(uname=uname,pwd=pwd)

        if userList:
            request.session['user'] = userList[0]
            red = request.POST.get('redirect','')
            if red == 'cart':

                #将session中的购物项移动到数据库
                SessionCartManager(request.session).migrateSession2DB()

                return HttpResponseRedirect('/cart/queryAll/')
            elif red == 'order':
                return HttpResponseRedirect('/order/order.html?cartitems='+request.POST.get('catitems',''))

            return HttpResponseRedirect('/user/center')
        return HttpResponseRedirect('/user/login')

class LoadCodeview(View):
    def get(self,request):
        img,str = gene_code()

        #将生成的验证码存放至session
        request.session['sessionCode'] = str

        return HttpResponse(img,content_type='image/png')


class CheckCodeview(View):
    def get(self,request):
        #获取输入框的验证码
        code = request.GET.get('code','')

        #获取生成的验证码
        sessionCode = request.session.get('sessionCode',None)

        #比较是否相等
        flag = code == sessionCode

        return JsonResponse({'checkFlag':flag})


class Addressview(View):
    def get(self,request):
        user = request.session.get('user', '')

        addrList = user.address_set.all()

        return render(request, 'address.html', {'addrList': addrList})

    def post(self,request):
        #获取请求参数
        aname = request.POST.get('aname','')
        aphone = request.POST.get('aphone','')
        addr = request.POST.get('addr','')
        user = request.session.get('user','')


        #将数据插入数据库
        address = Address.objects.create(aname=aname,aphone=aphone,addr=addr,userinfo=user,isdefault=(lambda count: True if count == 0 else False)(user.address_set.all().count()))


        #获取当前登录用户的所有收获地址
        addrList = user.address_set.all()

        return render(request,'address.html',{'addrList':addrList})

class LoadAreaview(View):
    def get(self,request):
        #获取请求参数
        pid = request.GET.get('pid',-1)
        pid = int(pid)

        #根据父ID查询区划信息
        areList = Area.objects.filter(parentid=pid)

        #进行序列化
        jareaList = serialize('json',areList)

        return JsonResponse({'jareaList':jareaList})
